<?php

declare(strict_types=1);

namespace app\seller\controller\admin;

use app\seller\model\NurseType;
use think\exception\ValidateException;
use think\Request;
use sheep\controller\Backend;
use app\seller\model\Nurse as NurseModel;
use app\seller\model\NurseType as NurseTypeModel;
use app\seller\validate\admin\Nurse as NurseValidate;
use app\operations\model\Store;

/**
 * 服务团队管理
 */
class Nurse extends Backend
{
    public $organ_id;
    const PAGE      = 1;
    const LIST_ROWS = 10;
    const ORDER     = 'DESC';
    const SORT      = 'id';

    private NurseTypeModel $nurse_type_model;

    public function initialize()
    {
        $this->organ_id = $this->shopId();
        $this->model = new NurseModel();
        $this->nurse_type_model = new NurseTypeModel();
    }


    /**
     * 服务团队列表
     *
     *
     * @return \think\Response
     */
    public function list(Request $request)
    {
        $params['page']      = $request->param('page', self::PAGE);
        $params['list_rows'] = $request->param('list_rows', self::LIST_ROWS);
        $params['search']    = json_decode($request->param('search', ''));
        $params['order']     = $request->param('order', self::ORDER);
        $params['sort']      = $request->param('sort', self::SORT);
        $params['id']        = $request->param('id', '');
        $params['organ_id']  = $this->organ_id;

        $brands = $this->model->list($params);
        return success('获取成功', $brands);
    }


    /**
     * 添加服务团队
     * param [
     * 'nurse_number' => 'string'
     * 'nurse_name' => 'string'
     * ]
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $nurses = $request->param('nurses', []);
        try {
            validate(NurseValidate::class)
                ->scene('add')
                ->check($nurses);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return error($e->getError());
        }
        $nurses['shop_id'] = $this->organ_id;
        $result = $this->model->dataCreate($nurses);
        if (!$result) {
            return error('保存失败');
        }
        return success('保存成功');
    }


    /**
     * 宠护员详情
     * $id 修改的服务团队id
     *
     * @param  $id
     * @return \think\Response
     */
    public function detail($id)
    {
        if (empty($id) || !is_numeric($id)) {
            return error('数据有误');
        }
        $brand = $this->model->nurseInfo($id);
        return success('获取成功', ['nurseInfo' => $brand]);
    }


    /**
     * 修改服务团队
     * $id 修改的宠护员id
     *
     * @return \think\Response
     */
    public function edit($id)
    {
        $nurses = $this->request->param('nurses', []);
        try {
            validate(NurseValidate::class)
                ->scene('edit')
                ->check($nurses);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return error($e->getError());
        }
        $result = $this->model->editCreate($nurses);
        if (!$result) {
            return error('更新失败');
        }
        return success('更新成功');
    }


    /**
     * 删除服务团队
     *
     * @param string $id 要删除的商品分类列表
     * @return void
     */
    public function delete($id)
    {
        $id = explode(',', $id);
        if (empty($id)) {
            return error('参数不能为空');
        }
        $result = $this->model->delData($id);
        if (!$result) {
            return error('更新失败');
        }
        return success('删除成功');
    }

    /**
     * 服务团队类型
     *
     * @param string
     * @return void
     */
    public function typeList()
    {
        $typeInfo = $this->nurse_type_model->typeInfo();
        return success('获取成功', $typeInfo);
    }

}
